<template>
  <div>
    <AModal
      v-bind="$attrs"
      width="600px"
      @close="modalEvent('close')"
      @ok="modalEvent('ok')"
      @open="modalEvent('open')"
      @cancel="modalEvent('cancel')"
    >
      <AForm ref="formRef" :model="form" auto-label-width>
        <ARow :gutter="10">
          <ACol>
            <AFormItem
              field="employeeName"
              label="员工名称"
              :rules="[{ required: true, message: '请输入员工名称' }]"
              :validate-trigger="['change', 'input', 'blur']"
            >
              <AInput v-model="form.employeeName" placeholder="请输入员工名称"></AInput>
            </AFormItem>
          </ACol>
          <ACol>
            <AFormItem field="employeeStatus" label="员工状态">
              <ASelect v-model="form.employeeStatus">
                <AOption label="在职" :value="0"></AOption>
                <AOption label="离职" :value="1"></AOption>
              </ASelect>
            </AFormItem>
          </ACol>
          <ACol>
            <AFormItem field="entryDate" label="入职时间">
              <ADatePicker v-model="form.entryDate" class="w100" :allow-clear="false"></ADatePicker>
            </AFormItem>
          </ACol>
          <ACol>
            <AFormItem field="quitDate" label="离职时间">
              <ADatePicker v-model="form.quitDate" class="w100"></ADatePicker>
            </AFormItem>
          </ACol>
        </ARow>
      </AForm>
    </AModal>
  </div>
</template>

<script lang="ts" setup>
import { Message } from '@arco-design/web-vue'
import { editEmployee, EmployeeReq, EmployeeResItem } from '@renderer/api/employee'
import { useMenuState } from '@renderer/store'
import { deleteNullObject } from '@renderer/utils/data-processing'
import dayjs from 'dayjs'

interface Props {
  [key: string]: any
  checkData?: Partial<EmployeeResItem>
}
const props = withDefaults(defineProps<Props>(), {
  checkData: () => {
    return {}
  }
})
const emit = defineEmits(['update:visible', 'reload'])
const attrs = useAttrs()
const formRef = ref()
const form = ref<EmployeeReq>({
  employeeNumber: '',
  employeeName: '',
  employeeStatus: 0,
  entryDate: dayjs().format('YYYY-MM-DD'),
  quitDate: ''
})
const menuParam = useMenuState()

// 弹窗事件
const modalEvent = (type: string) => {
  switch (type) {
    case 'ok':
      formRef.value.validate((errors: any) => {
        if (!errors) {
          // 新增
          if ((attrs.title as string).includes('新增')) {
            form.value.employeeId = undefined
          }
          editEmployee(deleteNullObject(form.value)).then(() => {
            Message.success((attrs.title as string).includes('新增') ? '新增成功' : '修改成功')
            modalEvent('cancel')
            emit('reload')
          })
        }
      })
      break
    case 'close':
      menuParam.switchEditStatus(false)
      formRef.value.resetFields()
      emit('update:visible', false)
      break
    case 'open':
      Object.assign(form.value, props.checkData)
      menuParam.switchEditStatus(true)
      break
    case 'cancel':
      formRef.value.resetFields()
      emit('update:visible', false)
      menuParam.switchEditStatus(false)
      break
    default:
      break
  }
}
</script>
<style scoped>
.card-container {
  margin-bottom: 10px;
}
</style>
